<html>
<head>
	<script src="./jquery-1.11.0.min.js"></script>
	    <meta charset="UTF-8"/>
</head>

<body>

描述:<input id="description" type="text" name="firstname" /> <br/>
允许评论:<input id="en_comment" type="checkbox" checked="checked" /> <br/>
允许评分:<input id="en_score" type="checkbox" checked="checked" /> <br/>
<input type="file" class="file" id="uploadFile" />
<input type="button" id="add_share" value="上传"/>
<div class="preview">
</div>

<script type="text/javascript">
	var fileArr=[];
	var count =0;
	var fileidArr=[];
	var enableNext = true;
	// image max 9, video max 1, not support image + video

	$("#uploadFile").on('change',function(event){
		console.log('change');
		var blobData = event.target.files[0];
		if(!blobData){
			console.log('null file select');
			return;
		}
		fileArr.push(blobData);

		var reader = new FileReader();
		reader.readAsDataURL(blobData);

		reader.onload=function(e){
			var fileData = e.target.result;
			if (blobData.type.match("image") !== null) {
				$('.preview').append("<img class='itemView' width='100' margin='1px'/> <br/>");
				$('.itemView')[count].src=fileData;
				count++;
			}else if (blobData.type.match("video") !== null){
				// video doc?
				$('.preview').append("<video class='itemView' controls='controls' preload='meta' width='320'>your browser not support video</video><br/>");
				$('.itemView')[count].src=fileData;
				count++;
			}else{
				// doc?
			}
		}
	});

	$("#add_share").on('click',function(){
		console.log('start load to qiNiu');
		// var file_data = $("#uploadFile").prop("files")[0];
		uploadAll(0,function(){
			console.log("all succ");
			console.log(fileidArr);
			addShare();
		},function(){
			console.log("error");
		})
	});

	function addShare(){
		var en_comment = $('#en_comment').is(":checked")?1:0;
		var en_score = $('#en_score').is(":checked")?1:0;
		var description = $('#description').val();
		var share = {
			employee_id:-1,
			en_comment:en_comment,
			en_score:en_score,
			description:description,
			type_flag:1,
			resource:parseResource()
		};
		console.log(share);
		// save

		$.ajax({
			url:'/infiniteus-sharefun/mobile/share',
			type:'POST',
			data:JSON.stringify(share),
			success:function(res){
				console.log(res);
				window.location.href="/infiniteus-sharefun/admin/share/list/1";
			},
			error:function(res){
				alert("fail");
			},
		});
	}

	function parseResource(){
		var resource = [];
		fileidArr.forEach(function(item){
			var obj = {};
			obj.file_name=$(item).find("file_name").text();
			obj.file_id=$(item).find("file_id").text();
			resource.push(obj);
		});
		return resource;
	}


	function uploadWaitResp(fileData, succ, error){
		var formData = new FormData();
		formData.append("fileToUpload", fileData);
		$.ajax({
			// url:"/infiniteus-sharefun/api/qiniuupload",
			url:"http://bsl.foreveross.com/infiniteus-sharefun/api/qiniuupload",
			type:"POST",
			processData: false,
			contentType: false,
			data: formData,
  			success: succ,
			error:error
		});
	}

	function uploadAll(index, succ, error){
		if(index >= fileArr.length){
			succ();
		}else{
			console.log('start upload index='+index);
			uploadWaitResp(fileArr[index], function(response){
				console.log('upload success index='+index);
				console.log(response);
				fileidArr.push(response);
				uploadAll(index+1, succ , error);
			},error);
		}
	}

</script>
</body>
</html>
